package com.atguigu.sugar.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.sugar.bean.*;
import com.atguigu.sugar.service.KeywordService;
import com.atguigu.sugar.service.ProductService;
import com.atguigu.sugar.service.ProvinceService;
import com.atguigu.sugar.service.VisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2022/1/7 10:11
 */
@RestController
public class SugarController {
    
    @Autowired
    ProductService product;
    
    @RequestMapping("/sugar/gmv")
    public String gmv(@RequestParam(value = "date", defaultValue = "0") int date) {
        date = checkDate(date);
        
        BigDecimal gmv = product.gmv(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        result.put("data", gmv);
        
        return result.toJSONString();
        
    }
    
    @RequestMapping("/sugar/gmv/c3")
    public String gmvByC3(@RequestParam(value = "date", defaultValue = "0") int date) {
        date = checkDate(date);
        
        List<Map<String, Object>> list = product.gmvByC3(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        for (Map<String, Object> map : list) {
            Object c3Name = map.get("category3_name");
            categories.add(c3Name);
        }
        
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        
        JSONObject obj = new JSONObject();
        obj.put("name", "商品品类");
        JSONArray data1 = new JSONArray();
        for (Map<String, Object> map : list) {
            
            data1.add(map.get("order_amount"));
        }
        
        obj.put("data", data1);
        
        series.add(obj);
        
        data.put("series", series);
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @RequestMapping("/sugar/gmv/spu")
    public String gmvBySpu(@RequestParam(value = "date", defaultValue = "0") int date) {
        date = checkDate(date);
        
        List<Spu> list = product.gmvBySpu(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONArray data = new JSONArray();
        for (Spu spu : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", spu.getSpu_name());
            obj.put("value", spu.getOrder_amount());
            data.add(obj);
        }
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @RequestMapping("/sugar/gmv/tm")
    public String gmvByTm(@RequestParam(value = "date", defaultValue = "0") int date) {
        date = checkDate(date);
        
        List<Tm> list = product.gmvByTm(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray columns = new JSONArray();
        
        JSONObject c1 = new JSONObject();
        c1.put("name", "品牌名");
        c1.put("id", "tm");
        columns.add(c1);
        
        JSONObject c2 = new JSONObject();
        c2.put("name", "销售额");
        c2.put("id", "amount");
        columns.add(c2);
        
        data.put("columns", columns);
        
        JSONArray rows = new JSONArray();
        for (Tm tm : list) {
            JSONObject row = new JSONObject();
            row.put("tm", tm.getTm_name());
            row.put("amount", tm.getOrder_amount());
            rows.add(row);
        }
        
        data.put("rows", rows);
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @Autowired
    ProvinceService provinceService;
    
    @RequestMapping("/sugar/province")
    public String statsProvince(@RequestParam(value = "date", defaultValue = "0") int date) {
        date = checkDate(date);
        
        List<Province> list = provinceService.statsByProvince(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        JSONArray mapData = new JSONArray();
        for (Province province : list) {
            
            JSONObject obj = new JSONObject();
            
            obj.put("name", province.getProvince_name());
            obj.put("value", province.getOrder_amount());
            
            JSONArray toolTipValues = new JSONArray();
            toolTipValues.add(province.getOrder_count());
            obj.put("tooltipValues", toolTipValues);
            
            mapData.add(obj);
            
        }
        
        data.put("mapData", mapData);
        
        data.put("valueName", "销售额");
        
        JSONArray toolTipNames = new JSONArray();
        toolTipNames.add("订单数");
        data.put("tooltipNames", toolTipNames);
        
        JSONArray toolTipUnits = new JSONArray();
        toolTipUnits.add("个");
        data.put("tooltipUnits", toolTipUnits);
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @Autowired
    VisitorService visitorService;
    
    @RequestMapping("/sugar/visitor/hour")
    public String statsByHour(@RequestParam(value = "date", defaultValue = "0") int date) {
        date = checkDate(date);
        
        List<Visitor> list = visitorService.statsByHour(date);
        // 转成一个map,方便后面根据小时查找对应的Visitor
        HashMap<Integer, Visitor> hourToVisitor = new HashMap<>();
        for (Visitor visitor : list) {
            hourToVisitor.put(visitor.getHour(), visitor);
        }
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        JSONArray categories = new JSONArray();
        for (int i = 0; i <= 23; i++) {
            categories.add(i);
        }
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        
        JSONObject pv = new JSONObject();
        pv.put("name", "pv");
        JSONArray pvData = new JSONArray();
        pv.put("data", pvData);
        series.add(pv);
        
        JSONObject uv = new JSONObject();
        uv.put("name", "uv");
        JSONArray uvData = new JSONArray();
        uv.put("data", uvData);
        series.add(uv);
        
        JSONObject uj = new JSONObject();
        uj.put("name", "uj");
        JSONArray ujData = new JSONArray();
        uj.put("data", ujData);
        series.add(uj);
        
        for (int hour = 0; hour <= 23; hour++) {
            // 根据hour去找对应的的Visitor对象
            Visitor visitor = hourToVisitor.get(hour);
            if (visitor != null) {
                pvData.add(visitor.getPv());
                uvData.add(visitor.getUv());
                ujData.add(visitor.getUj());
            } else {
                pvData.add(0);
                uvData.add(0);
                ujData.add(0);
            }
        }
        data.put("series", series);
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @Autowired
    KeywordService keywordService;
    
    @RequestMapping("/sugar/kw")
    public String statsKw(@RequestParam(value = "date", defaultValue = "0") int date) {
        date = checkDate(date);
        
        List<Keyword> keywords = keywordService.statsKw(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONArray data = new JSONArray();
        for (Keyword keyword : keywords) {
            JSONObject obj = new JSONObject();
            obj.put("name", keyword.getKeyword());
            obj.put("value", keyword.getScore());
            
            data.add(obj);
        }
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    private int checkDate(int date) {
        if (date == 0) { // 表示没有传递日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        return date;
    }
    
}
